package com.cloudservice.ccs.websocket;

import com.cloudservice.ccs.util.WebSocketSessionPool;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/notice-and-system-message/")
@Component
public class NoticeAndSystemMessageServer {

    private static WebSocketSessionPool<Integer> sessionPool = new WebSocketSessionPool<>();

    public static WebSocketSessionPool<Integer> getSessionPool() {
        return sessionPool;
    }

    @OnOpen
    public void onOpen(Session session) {
        sessionPool.addSession(sessionPool.getOnlineCount().intValue(), session);
    }

    @OnClose
    public void onClose(Session session) {
        sessionPool.removeSession(session);
    }

    @OnError
    public void onError(Throwable throwable) {
        throwable.printStackTrace();
    }
}
